'use strict';

define(['controllers', '../service/roleService'], function (controllers) {
    controllers.system.register('RoleController', ['$scope', '$q', 'RoleService', function ($scope, $q, roleService) {
        $scope.init = function () {
            $scope.pageNo = 1;
            $scope.pageSize = 10;
        };

        $scope.initEvent = function () {
            $('#editDialog').on('shown.bs.modal', function (e) {
                $('#save_name').focus();
            });
        };

        $scope.initEditForm = function () {
            $scope.save_name = '';
            for (var selectPrivilegeId in $scope.privilegeCheckedMap) {
                $scope.privilegeCheckedMap[selectPrivilegeId] = false;
            }
        };

        $scope.query = function (pageNo) {
            if (pageNo != null) {
                $scope.pageNo = pageNo;
            }
            $scope.paged = roleService.query({
                name: $scope.query_name,
                pageNo: $scope.pageNo,
                pageSize: $scope.pageSize
            });
        };

        $scope.togglePrivilegeChk = function (privilegeId) {
            $scope.privilegeCheckedMap[privilegeId] = !$scope.privilegeCheckedMap[privilegeId];
            var subPrivileges = $scope.privilegeMap[privilegeId].children;
            if (subPrivileges != null) {
                for (var i = 0; i < subPrivileges.length; i++) {
                    $scope.privilegeCheckedMap[subPrivileges[i].id] = $scope.privilegeCheckedMap[privilegeId];
                }
            }
        };

        $scope.openEditDialog = function (id, name) {
            $scope.privilegeMap = {};
            $scope.privilegeCheckedMap = {};
            $scope.privileges = roleService.getPrivileges(function (data) {
                for(var i = 0; i < data.length; i++) {
                    var privilege = data[i];
                    $scope.privilegeMap[privilege.id] = privilege;
                    if (privilege.children != null) {
                        for (var j = 0; j < privilege.children.length; j++) {
                            $scope.privilegeMap[privilege.children[j].id] = privilege.children[j];
                        }
                    }
                }
            });
            if (id != null) {
                $scope.selectId = id;
                $scope.save_name = name;
                $scope.rolePrivileges = roleService.getPrivilegesByRoleId({id: id}, function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var rolePrivilege = data[i];
                        $scope.privilegeCheckedMap[rolePrivilege.id] = true;
                        if (rolePrivilege.parentId != null) {
                            $scope.privilegeCheckedMap[rolePrivilege.parentId] = true;
                        }
                    }
                });
            } else {
                $scope.selectId = null;
                $scope.save_name = "";
                $scope.rolePrivileges = null;
            }

            $('#editDialog').modal('show');
        };

        $scope.save = function() {
            $scope.validName(false, function() {
                var selectPrivilegeIds = [];
                for (var selectPrivilegeId in $scope.privilegeCheckedMap) {
                    if ($scope.privilegeCheckedMap[selectPrivilegeId]) {
                        selectPrivilegeIds.push(selectPrivilegeId);
                    }
                }
                roleService.save({
                    id: $scope.selectId
                }, {
                    name: $scope.save_name,
                    privilegeIds: selectPrivilegeIds
                }, function() {
                    $scope.query();
                    $scope.initEditForm();
                    $('#editDialog').modal('hide');
                });
            });
        };

        $scope.validName = function(allowEmpty, callback) {
            $('#editErrorMsg').addClass('hide');
            $('#save_name_group').removeClass('has-error');

            if (isEmpty($scope.save_name)) {
                if (!allowEmpty) {
                    $('#editErrorMsg').html('名称不能为空').removeClass('hide');
                    $('#save_name_group').addClass('has-error');
                }
                return;
            }

            var exist = function () {
                var defered = $q.defer();
                roleService.getByName({
                    name: $scope.save_name,
                    idNo: $scope.selectId
                }, function (data) {
                    if (data.id == null) {
                        defered.resolve('ok');
                    } else {
                        defered.reject('账号已存在，请重新输入');
                    }
                });
                return defered.promise;
            };

            var promise = exist();
            promise.then(function() {
                if (callback != null) {
                    callback();
                }
            }, function(err) {
                $('#editErrorMsg').html(err).removeClass('hide');
                $('#save_name_group').addClass('has-error');
            });
        };

        $scope.init();
        $scope.initEvent();
        $scope.query();
    }]);
});
